import * as THREE from 'three';
import { Vec3 } from '@/core/types';

/**
 * 玩家3D模型类 - 用于第三人称视角显示
 */
export class PlayerModel {
  private group: THREE.Group;
  private head!: THREE.Mesh;
  private body!: THREE.Mesh;
  private leftArm!: THREE.Mesh;
  private rightArm!: THREE.Mesh;
  private leftLeg!: THREE.Mesh;
  private rightLeg!: THREE.Mesh;
  
  // 动画相关
  private walkAnimation = 0;
  private armSwingAnimation = 0;
  private isWalking = false;

  constructor() {
    this.group = new THREE.Group();
    this.createPlayerModel();
  }

  private createPlayerModel(): void {
    // 创建玩家皮肤材质
    const skinMaterial = new THREE.MeshLambertMaterial({ color: 0xFDBCB4 }); // 肤色
    const clothesMaterial = new THREE.MeshLambertMaterial({ color: 0x4A90E2 }); // 蓝色衣服
    const pantsMaterial = new THREE.MeshLambertMaterial({ color: 0x2C3E50 }); // 深色裤子
    const shoesMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 }); // 棕色鞋子

    // 头部
    const headGeometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
    this.head = new THREE.Mesh(headGeometry, skinMaterial);
    this.head.position.set(0, 1.625, 0); // 头部位置
    this.group.add(this.head);

    // 身体
    const bodyGeometry = new THREE.BoxGeometry(0.5, 0.75, 0.25);
    this.body = new THREE.Mesh(bodyGeometry, clothesMaterial);
    this.body.position.set(0, 1.125, 0); // 身体位置
    this.group.add(this.body);

    // 左臂
    const armGeometry = new THREE.BoxGeometry(0.25, 0.75, 0.25);
    this.leftArm = new THREE.Mesh(armGeometry, skinMaterial);
    this.leftArm.position.set(-0.375, 1.125, 0);
    this.leftArm.geometry.translate(0, -0.375, 0); // 设置旋转中心
    this.group.add(this.leftArm);

    // 右臂
    this.rightArm = new THREE.Mesh(armGeometry, skinMaterial);
    this.rightArm.position.set(0.375, 1.125, 0);
    this.rightArm.geometry.translate(0, -0.375, 0); // 设置旋转中心
    this.group.add(this.rightArm);

    // 左腿
    const legGeometry = new THREE.BoxGeometry(0.25, 0.75, 0.25);
    this.leftLeg = new THREE.Mesh(legGeometry, pantsMaterial);
    this.leftLeg.position.set(-0.125, 0.375, 0);
    this.leftLeg.geometry.translate(0, -0.375, 0); // 设置旋转中心
    this.group.add(this.leftLeg);

    // 右腿
    this.rightLeg = new THREE.Mesh(legGeometry, pantsMaterial);
    this.rightLeg.position.set(0.125, 0.375, 0);
    this.rightLeg.geometry.translate(0, -0.375, 0); // 设置旋转中心
    this.group.add(this.rightLeg);

    // 添加鞋子
    const shoeGeometry = new THREE.BoxGeometry(0.3, 0.1, 0.4);
    const leftShoe = new THREE.Mesh(shoeGeometry, shoesMaterial);
    leftShoe.position.set(-0.125, 0.05, 0.05);
    this.group.add(leftShoe);

    const rightShoe = new THREE.Mesh(shoeGeometry, shoesMaterial);
    rightShoe.position.set(0.125, 0.05, 0.05);
    this.group.add(rightShoe);

    // 设置阴影
    this.group.traverse((child) => {
      if (child instanceof THREE.Mesh) {
        child.castShadow = true;
        child.receiveShadow = true;
      }
    });
  }

  public getMesh(): THREE.Group {
    return this.group;
  }

  public setPosition(position: Vec3): void {
    this.group.position.set(position.x, position.y, position.z);
  }

  public setRotation(yRotation: number): void {
    this.group.rotation.y = yRotation;
  }

  public updateAnimation(deltaTime: number, velocity: Vec3, armSwing: number = 0): void {
    // 检查是否在行走
    const speed = Math.sqrt(velocity.x * velocity.x + velocity.z * velocity.z);
    this.isWalking = speed > 0.1;

    if (this.isWalking) {
      // 行走动画
      this.walkAnimation += deltaTime * 8; // 动画速度
      
      // 腿部摆动
      const legSwing = Math.sin(this.walkAnimation) * 0.5;
      this.leftLeg.rotation.x = legSwing;
      this.rightLeg.rotation.x = -legSwing;
      
      // 手臂摆动（与腿部相反）
      const armSwing = Math.sin(this.walkAnimation) * 0.3;
      this.leftArm.rotation.x = -armSwing;
      this.rightArm.rotation.x = armSwing;
    } else {
      // 静止时重置动画
      this.leftLeg.rotation.x = 0;
      this.rightLeg.rotation.x = 0;
      this.leftArm.rotation.x = 0;
      this.rightArm.rotation.x = 0;
    }

    // 手臂挥动动画（破坏方块时）
    if (armSwing > 0) {
      this.rightArm.rotation.x = -armSwing;
    }
  }

  public dispose(): void {
    this.group.traverse((child) => {
      if (child instanceof THREE.Mesh) {
        if (child.geometry) {
          child.geometry.dispose();
        }
        if (child.material) {
          if (Array.isArray(child.material)) {
            child.material.forEach(mat => mat.dispose());
          } else {
            child.material.dispose();
          }
        }
      }
    });
  }
}